package com.neu.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.metadata.Sheet;
import com.neu.constant.MaterialTypeEnum;
import com.neu.constant.ResultCodeEnum;
import com.neu.mapper.ArticleMapper;
import com.neu.mapper.CooperatorMapper;
import com.neu.pojo.Article;
import com.neu.pojo.Cooperator;
import com.neu.service.CooperatorService;
import com.neu.service.MaterialService;
import com.neu.utils.ExcelListener;
import com.neu.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Slf4j
@Service
public class CooperatorServiceImpl implements CooperatorService {

    @Resource
    private CooperatorMapper cooperatorMapper;

    @Resource
    private ArticleMapper articleMapper;

    @Resource
    private MaterialService materialService;

    @Override
    public List<Cooperator> queryCooperatorList(int provinceId) {
        return cooperatorMapper.queryCooperatorList(provinceId);
    }

    @Override
    public int addCooperator(Cooperator cooperator) {
        Article article = Article.builder().title(cooperator.getName()).content("").build();
        articleMapper.insertArticle(article);
        cooperator.setText(article.getId());
        return cooperatorMapper.insertCooperator(cooperator);
    }

    @Override
    public int deleteCooperator(Cooperator cooperator) {
        materialService.deleteMaterialGroup(cooperator.getId(), MaterialTypeEnum.ARTICLE.type);
        /*
            服务器文件数据删除待补充...
         */
        articleMapper.deleteArticle(cooperator.getText());
        return cooperatorMapper.deleteCooperator(cooperator.getId());
    }

    @Override
    public int updateCooperator(Cooperator cooperator) {
        return cooperatorMapper.updateCooperator(cooperator);
    }

    @Override
    public Object batchInsertCooperator(MultipartFile file, int provinceId) throws IOException {
        ExcelReader excelReader = null;
        InputStream in = null;
        ExcelListener listener = new ExcelListener();
        try {
            in=file.getInputStream();
            excelReader = EasyExcel.read(in,Cooperator.class,listener).build();
            excelReader.read(EasyExcel.readSheet(0).build());
            for(Object o:listener.getData()){
                Cooperator c=(Cooperator) o;
                c.setProvinceId(provinceId);
                Article article = Article.builder().title(c.getName()).content("").build();
                articleMapper.insertArticle(article);
                c.setText(article.getId());
                cooperatorMapper.insertCooperator(c);
            }
        } catch (Exception e) {
            log.error("导入失败",e);
            return Result.error(ResultCodeEnum.EXCEL_ERROR);
        } finally {
            in.close();
            // 这里一定别忘记关闭，读的时候会创建临时文件，到时磁盘会崩
            if (excelReader != null) {
                excelReader.finish();
            }
        }
        return 0;
    }
}
